package com.ssh.demo.netty.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ClassName TcpNettyDecoder
 * @Description TODO
 * @Author ssh
 * @Date 2021/4/12 13:47
 */
@Slf4j
@AllArgsConstructor
public class TcpNettyDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        System.out.println("进行解码："+msg.toString());
        System.out.println("byteBuf的容量为："+msg.capacity());
        System.out.println("byteBuf的可读容量为："+msg.readableBytes());
        System.out.println("byteBuf的可写容量为："+msg.writableBytes());
        byte[] data = new byte[msg.readableBytes()];
        //读取核心的数据
        msg.readBytes(data);
        out.add(data);
    }


}
